﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using SDLL;
using SGUI;

namespace SABI
{
    public partial class frm0003 : Form
    {
        public frm0003()
        {
            InitializeComponent();
        }

        string sql = "";
        int nextMonth, nextYear;

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }
               

        private void frm0003_Load(object sender, EventArgs e)
        {

        }

        private void frm0003_Shown(object sender, EventArgs e)
        {
            loadDATA();
        }

        void loadDATA()
        {
            GUI.ChangeDateFormat(DTPFromDate, FRM.SystemDateFormat);
            GUI.ChangeDateFormat(DTPToDate, FRM.SystemDateFormat);

            txtDivisionID.Text = FRM.DivisionID;
            txtMonth.Text = FRM.Month.PadLeft(2, '0') + "/" + FRM.Year;
            if(int.Parse(FRM.Month) + 1 > 12)
            {
                nextMonth = 1;
                nextYear = int.Parse(FRM.Year) + 1;
            }
            else
            {
                nextMonth = int.Parse(FRM.Month) + 1;
                nextYear = int.Parse(FRM.Year);
            }
            txtNextMonth.Text = nextMonth.ToString().PadLeft(2, '0') + "/" + nextYear.ToString();
            DateTime dtFrom = new DateTime(nextYear, nextMonth, 1);
            DateTime dtTo = dtFrom.AddMonths(1).AddDays(-1);
            DTPFromDate.Value = dtFrom;
            DTPToDate.Value = dtTo;
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            if (DTPFromDate.Value > DTPToDate.Value)
            {
                DLL02.Alert("Thời gian từ ngày đến ngày không hợp lệ!");
                return;
            }
            // Kiểm tra có kỳ kế toán tiếp theo chưa?

            sql = string.Format("Select * From tb0199 Where DivisionID = '{0}' and TranMonth = {1} and TranYear = {2}",FRM.DivisionID,nextMonth,nextYear);
            DataTable tb0199 = DATA.GetDataTable(sql, FRM.ConnectString);
            if (tb0199.Rows.Count == 0)
            {
                sql = string.Format("EXEC pr0092 '{0}','ketoan','{1}', ", FRM.DivisionID, txtNextMonth.Text);
                sql += string.Format(" ' Kỳ kế toán {0}', 'PE','{1:MM/dd/yyyy}' ,'{2}','A'", txtNextMonth.Text, DateTime.Now, FRM.UserName);

                DATA.ExecuteNonQuery(sql, FRM.ConnectString);
            }

            sql = string.Format("Exec pr0365 '{0}', {1}, {2},'{3:MM/dd/yyyy}','{4:MM/dd/yyyy}'", FRM.DivisionID, FRM.Month, FRM.Year, DTPFromDate.Value, DTPToDate.Value);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);

            sql = string.Format("Exec pr4502 '{0}', {1}, {2},'{3:MM/dd/yyyy}','{4:MM/dd/yyyy}'", FRM.DivisionID, FRM.Month, FRM.Year, DTPFromDate.Value, DTPToDate.Value);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);

            sql = string.Format("Exec pr2000 '{0}', {1}, {2},'{3:MM/dd/yyyy}','{4:MM/dd/yyyy}'", FRM.DivisionID, FRM.Month, FRM.Year, DTPFromDate.Value, DTPToDate.Value);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);

            sql = string.Format("Exec pr3600 '{0}', {1}, {2},'{3:MM/dd/yyyy}','{4:MM/dd/yyyy}'", FRM.DivisionID, FRM.Month, FRM.Year, DTPFromDate.Value, DTPToDate.Value);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);

            sql = string.Format("Exec pr3200 '{0}', {1}, {2},'{3:MM/dd/yyyy}','{4:MM/dd/yyyy}'", FRM.DivisionID, FRM.Month, FRM.Year, DTPFromDate.Value, DTPToDate.Value);
            DATA.ExecuteNonQuery(sql, FRM.ConnectString);

            FRM.Month = nextMonth.ToString();
            FRM.Year = nextYear.ToString();
            FRM.Period = txtNextMonth.Text;
            FRM.DateDue = new DateTime(nextYear, nextMonth, 1);

            DATA.ExecuteNonQuery(string.Format("Update tb0199 set Active=0 Where DivisionID='{0}'",FRM.DivisionID),FRM.ConnectString);
            DATA.ExecuteNonQuery(string.Format("Update tb0199 set Active=1 Where TranMonth={0} And TranYear={1} And DivisionID='{2}'", FRM.Month, FRM.Year, FRM.DivisionID), FRM.ConnectString);

            DLL02.Info("Khóa kỳ kế toán " + txtMonth.Text + " thành công");

            (Application.OpenForms["frmHome"] as frmHOME).loadGUI_Form();
        }

    }
}
